package com.twetec.cms.common.util.time;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    private static final Logger logger = LoggerFactory.getLogger(TimeUtil.class);

    public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";

    public static Date getEndTime(Date endTime){
        if (null == endTime){
            return null;
        }
        Date parse = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            logger.info("结束时间 {}", formatter.format(endTime));
            SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            parse = formatter2.parse(formatter.format(endTime) + " 23:59:59");
        }catch (Exception e){
          logger.info("时间转换异常 {}", e);
        }
        return parse;
    }

    /**
     * 时间比较
     *
     * @param date_time1
     * @param date_time2
     * @return
     */
    public static int compare(String date_time1, String date_time2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        try {
            cal1.setTime(new SimpleDateFormat(FORMAT_DATETIME).parse(date_time1.length() < 19 ? date_time1 + " 00:00:00" : date_time1));
            cal2.setTime(new SimpleDateFormat(FORMAT_DATETIME).parse(date_time2.length() < 19 ? date_time2 + " 00:00:00" : date_time2));
        } catch (ParseException e) {
            throw new RuntimeException("日期时间格式错误");
        }
        return cal1.compareTo(cal2);
    }
/*
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
        Date endTime = getEndTime(date);
        System.out.println(simpleDateFormat.format(endTime));
        System.out.println(compare(simpleDateFormat.format(endTime),simpleDateFormat.format(date)));
    }*/
}
